// test/pages/ticketPayInfo/ticketPayInfo.js
const api = require("../../../utils/api.js")
const app = getApp()
let teamList = [{
    team_name: "请选择代表队",
    id: "select"
}]
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ticketPrice: "",
        nums: "",
        dataList: {},
        teamList: [],
        currentId: "select",
        name: "",
        phone: "",
        currentTimes: [],
        currentType: [],
        index: 0, //选择的下拉列表下标
        enableClick:true
    },
    optionTap(e){
        const index =  e.detail.value;
        this.setData({
            index,
            currentId:this.data.teamList[index].id
        })
        
    },
    // 获取填写的姓名
    inputName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    inputPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    // 去买票
    gotoBuyTicket() {
        if (this.data.currentId === "select") {
            wx.showToast({
                title: '请选择代表队',
                icon: "none"
            })
            return
        }
        if (!this.data.name) {
            wx.showToast({
                title: '姓名不能为空',
                icon: "none"
            })
            return
        }
        let rules = /^1(3|4|5|6|7|8|9)(\d){9}/
        let result = rules.test(this.data.phone)
        if (!result) {
            wx.showToast({
                title: '电话号码格式不正确',
                icon: "none"
            })
            return
        }
        this.setData({
            enableClick:false
        })
        wx.request({
            url: app.globalData.host + "Ticket/wxpay",
            data: {
                token: wx.getStorageSync('token'),
                com_id: this.data.com_id,
                ticket_id: this.data.currentType[0].id,
                price: this.data.ticketPrice,
                num: this.data.nums,
                team_id: this.data.currentId,
                leader: this.data.name,
                phone: this.data.phone
            },
            method: "POST",
            success: (res) => {
                if (res.data.code === 2000) {
                    const paySignRes = res.data.data
                    wx.requestPayment({
                        nonceStr: paySignRes.nonceStr,
                        package: paySignRes.package,
                        paySign: paySignRes.paySign,
                        timeStamp: paySignRes.timeStamp,
                        signType: paySignRes.signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                            })
                            setTimeout(() => {
                                wx.navigateTo({
                                    url: `/test/pages/check_ticket_order/check_ticket_order?order_id=${paySignRes.order_no}`
                                })
                            }, 1000)

                        },
                        fail: (err) => {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none'
                            })
                            this.setData({
                                enableClick:true
                            })
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                    this.setData({
                        enableClick:true
                    })
                }
            },
            fail: (err) => {
                this.setData({
                    enableClick:true
                })
            }
        })

    },
    // 获取初始数据
    getDataList() {
        wx.request({
            url: app.globalData.host + "Ticket/ticket_info",
            method: "POST",
            data: {
                com_id: this.data.com_id,
                token: wx.getStorageSync('token')
            },
            success: (res) => {
                if (res.data.code === 2000) {
                    this.setData({
                        dataList: res.data.data[0]
                    })
                    const currentTimes = this.data.dataList.com_ticket_info_times.filter(item => {
                        return item.id == this.data.sid
                    })
                    this.setData({
                        currentTimes
                    })
                    this.getType()
                    this.getTeamList()
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                }
            },
            fail: (err) => {
                console.log(err);
            }
        })
    },
    // 根据场次获取id
    async getType() {
        const res = await api.fetchApi("Ticket/get_type", {
            com_id: this.data.com_id,
            times: this.data.currentTimes[0].times
        })
        const currentType = res.filter(item => {
            return item.id == this.data.tid
        })
        this.setData({
            currentType
        })
    },
    // 获取代表队信息
    async getTeamList() {
        try {
            const res = await api.fetchApi("Ticket/get_team", {
                com_id: this.data.com_id
            })
            teamList = [...teamList, ...res]
            this.setData({
                teamList,
            })
        } catch (error) {
            console.log(error);
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.setData(options)
        this.getDataList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        teamList = [{
            team_name: "请选择代表队",
            id: "select"
        }]
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})